home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH10 / SRC / OBJTRANS.CLS < prev    next >
Encoding:
Text File  |  1996-05-04  |  8.3 KB  |  292 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "ObjTransformed"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. Private NumCurvePts As Integer
  11. Private CurvePoints() As Point3D
  12.  
  13. Private NumTrans As Integer
  14. Private Trans() As Transformation
  15.  
  16. Private pline As ObjPolyline    ' The display polyline.
  17.  
  18. ' ************************************************
  19. ' Add a point to the curve.
  20. ' ************************************************
  21. Public Sub AddCurvePoint(x As Single, y As Single, z As Single)
  22.     NumCurvePts = NumCurvePts + 1
  23.     ReDim Preserve CurvePoints(1 To NumCurvePts)
  24.     CurvePoints(NumCurvePts).coord(1) = x
  25.     CurvePoints(NumCurvePts).coord(2) = y
  26.     CurvePoints(NumCurvePts).coord(3) = z
  27.     CurvePoints(NumCurvePts).coord(4) = 1
  28. End Sub
  29.  
  30.  
  31. ' ************************************************
  32. ' Set a transformation.
  33. ' ************************************************
  34. Public Sub SetTrans(M() As Single)
  35.     NumTrans = NumTrans + 1
  36.     ReDim Preserve Trans(1 To NumTrans)
  37.     m3MatCopy Trans(NumTrans).M, M
  38. End Sub
  39.  
  40. ' ************************************************
  41. ' Create the display polyline by applying the
  42. ' series of transformations in array M().
  43. ' ************************************************
  44. Public Sub Transform()
  45. Dim i As Integer
  46. Dim j As Integer
  47. Dim x0 As Single
  48. Dim y0 As Single
  49. Dim z0 As Single
  50. Dim x1 As Single
  51. Dim y1 As Single
  52. Dim z1 As Single
  53. Dim x2 As Single
  54. Dim y2 As Single
  55. Dim z2 As Single
  56.  
  57.     Set pline = New ObjPolyline
  58.         
  59.     ' Add the original curve to pline.
  60.     x1 = CurvePoints(1).coord(1)
  61.     y1 = CurvePoints(1).coord(2)
  62.     z1 = CurvePoints(1).coord(3)
  63.     For j = 2 To NumCurvePts
  64.         x2 = CurvePoints(j).coord(1)
  65.         y2 = CurvePoints(j).coord(2)
  66.         z2 = CurvePoints(j).coord(3)
  67.         pline.AddSegment x1, y1, z1, x2, y2, z2
  68.         x1 = x2
  69.         y1 = y2
  70.         z1 = z2
  71.     Next j
  72.     
  73.     ' Start with the transformed coordinates
  74.     ' the same as the original coordinates.
  75.     For j = 1 To NumCurvePts
  76.         CurvePoints(j).Trans(1) = CurvePoints(j).coord(1)
  77.         CurvePoints(j).Trans(2) = CurvePoints(j).coord(2)
  78.         CurvePoints(j).Trans(3) = CurvePoints(j).coord(3)
  79.     Next j
  80.     
  81.     ' Create the transformed copies of the curve.
  82.     For i = 1 To NumTrans
  83.         ' Place the first point.
  84.         x1 = CurvePoints(1).Trans(1)
  85.         y1 = CurvePoints(1).Trans(2)
  86.         z1 = CurvePoints(1).Trans(3)
  87.         m3ApplyFull _
  88.             CurvePoints(1).coord, Trans(i).M, _
  89.             CurvePoints(1).Trans
  90.         x0 = CurvePoints(1).Trans(1)
  91.         y0 = CurvePoints(1).Trans(2)
  92.         z0 = CurvePoints(1).Trans(3)
  93.         pline.AddSegment x1, y1, z1, x0, y0, z0
  94.         
  95.         ' Add the rest of the points.
  96.         For j = 2 To NumCurvePts
  97.             x1 = CurvePoints(j).Trans(1)
  98.             y1 = CurvePoints(j).Trans(2)
  99.             z1 = CurvePoints(j).Trans(3)
  100.             m3ApplyFull _
  101.                 CurvePoints(j).coord, Trans(i).M, _
  102.                 CurvePoints(j).Trans
  103.             x2 = CurvePoints(j).Trans(1)
  104.             y2 = CurvePoints(j).Trans(2)
  105.             z2 = CurvePoints(j).Trans(3)
  106.             ' (x0, y0, z0) = previous point, new.
  107.             ' (x1, y1, z1) = current point, old.
  108.             ' (x2, y2, z2) = current point, new.
  109.             pline.AddSegment x0, y0, z0, x2, y2, z2
  110.             pline.AddSegment x1, y1, z1, x2, y2, z2
  111.             x0 = x2
  112.             y0 = y2
  113.             z0 = z2
  114.         Next j
  115.     Next i
  116. End Sub
  117.  
  118. ' ***********************************************
  119. ' Return a string indicating the object type.
  120. ' ***********************************************
  121. Property Get ObjectType() As String
  122.     ObjectType = "TRANSFORMED"
  123. End Property
  124.  
  125.  
  126.  
  127. ' ***********************************************
  128. ' Fix the data coordinates at their transformed
  129. ' values.
  130. ' ***********************************************
  131. Public Sub FixPoints()
  132. Dim i As Integer
  133. Dim j As Integer
  134.  
  135.     ' Fix the curve points.
  136.     For i = 1 To NumCurvePts
  137.         For j = 1 To 3
  138.             CurvePoints(i).coord(j) = CurvePoints(i).Trans(j)
  139.         Next j
  140.     Next i
  141.  
  142.     ' Fix the display polyline if it exists.
  143.     If Not pline Is Nothing Then pline.FixPoints
  144. End Sub
  145.  
  146. ' ************************************************
  147. ' Apply a transformation matrix which may not
  148. ' contain 0, 0, 0, 1 in the last column to the
  149. ' object.
  150. ' ************************************************
  151. Public Sub ApplyFull(M() As Single)
  152. Dim i As Integer
  153.  
  154.     ' Transform the curve.
  155.     For i = 1 To NumCurvePts
  156.         m3ApplyFull CurvePoints(i).coord, M, _
  157.                     CurvePoints(i).Trans
  158.     Next i
  159.     
  160.     ' Transform the display polyline if it exists.
  161.     If Not pline Is Nothing Then pline.ApplyFull M
  162. End Sub
  163.  
  164. ' ************************************************
  165. ' Apply a transformation matrix to the object.
  166. ' ************************************************
  167. Public Sub Apply(M() As Single)
  168. Dim i As Integer
  169.  
  170.     ' Transform the curve.
  171.     For i = 1 To NumCurvePts
  172.         m3Apply CurvePoints(i).coord, M, _
  173.                 CurvePoints(i).Trans
  174.     Next i
  175.     
  176.     ' Transform the display polyline if it exists.
  177.     If Not pline Is Nothing Then pline.Apply M
  178. End Sub
  179.  
  180.  
  181. ' ************************************************
  182. ' Apply a nonlinear transformation.
  183. ' ************************************************
  184. Public Sub Distort(D As Object)
  185. Dim i As Integer
  186.  
  187.     ' Distort the curve.
  188.     For i = 1 To NumCurvePts
  189.         D.Distort CurvePoints(i).coord(1), _
  190.                   CurvePoints(i).coord(2), _
  191.                   CurvePoints(i).coord(3)
  192.     Next i
  193.     
  194.     ' Distort the display polyline if it exists.
  195.     If Not pline Is Nothing Then pline.Distort D
  196. End Sub
  197.  
  198.  
  199. ' ************************************************
  200. ' Write the surface's display polyline object to a
  201. ' file using Write. The data can later be loaded
  202. ' into an ObjPolyline object but not an
  203. ' ObjRotated object.
  204. ' ************************************************
  205. Public Sub FileWritePolyline(filenum As Integer)
  206.     If Not pline Is Nothing Then pline.FileWrite filenum
  207. End Sub
  208.  
  209.  
  210. ' ************************************************
  211. ' Write an extruded surface to a file using Write.
  212. ' Begin with "TRANSFORMED" to identify this object.
  213. ' ************************************************
  214. Public Sub FileWrite(filenum As Integer)
  215. Dim i As Integer
  216. Dim j As Integer
  217. Dim k As Integer
  218.  
  219.     ' Write basic information.
  220.     Write #filenum, "TRANSFORMED", NumCurvePts, _
  221.         NumTrans
  222.         
  223.     ' Write the curve points.
  224.     For i = 1 To NumCurvePts
  225.         Write #filenum, _
  226.             CurvePoints(i).coord(1), _
  227.             CurvePoints(i).coord(2), _
  228.             CurvePoints(i).coord(3)
  229.     Next i
  230.     
  231.     ' Write the transformations.
  232.     For i = 1 To NumTrans
  233.         For j = 1 To 4
  234.             For k = 1 To 4
  235.                 Write #filenum, Trans(i).M(j, k)
  236.             Next k
  237.         Next j
  238.     Next i
  239. End Sub
  240.  
  241.  
  242.  
  243.  
  244. ' ************************************************
  245. ' Draw the extrusion on a Form, Printer, or
  246. ' PictureBox.
  247. ' ************************************************
  248. Public Sub Draw(canvas As Object, Optional R As Variant)
  249.     If Not pline Is Nothing Then _
  250.         pline.Draw canvas, R
  251. End Sub
  252.  
  253.  
  254. ' ************************************************
  255. ' Read a grid from a file using Input.
  256. ' Assume the "TRANSFORMED" label has already been
  257. ' read.
  258. ' ************************************************
  259. Public Sub FileInput(filenum As Integer)
  260. Dim i As Integer
  261. Dim j As Integer
  262. Dim k As Integer
  263.  
  264.     ' Get the basic information.
  265.     Input #filenum, NumCurvePts, NumTrans
  266.     
  267.     ' Allocate and read the curve array.
  268.     ReDim CurvePoints(1 To NumCurvePts)
  269.     For i = 1 To NumCurvePts
  270.         Input #filenum, _
  271.             CurvePoints(i).coord(1), _
  272.             CurvePoints(i).coord(2), _
  273.             CurvePoints(i).coord(3)
  274.         CurvePoints(i).coord(4) = 1
  275.     Next i
  276.     
  277.     ' Allocate and read the transformations.
  278.     ReDim Trans(1 To NumTrans)
  279.     For i = 1 To NumTrans
  280.         For j = 1 To 4
  281.             For k = 1 To 4
  282.                 Input #filenum, Trans(i).M(j, k)
  283.             Next k
  284.         Next j
  285.     Next i
  286.     
  287.     ' Create the display polyline.
  288.     Transform
  289. End Sub
  290.  
  291.  
  292.